const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
app.commandLine.appendSwitch('disable-features', 'OutOfBlinkCors');
if (app.isPackaged) {
  require('./serve.js');
}

function createWindow() {
  const win = new BrowserWindow({
    width: 1280,
    height: 720,
    frame: false,
    // titleBarStyle: 'hidden',
    // titleBarOverlay: {
    //   color: '#fff',
    //   symbolColor: 'black',
    // },
    // transparent: true,
    // backgroundColor: 'rgba(255,255,255,0)',
    useContentSize: true,
    focusable: true,
    alwaysOnTop: false,
    webPreferences: {
      webSecurity: false,
      nodeIntegration: true,
      contextIsolation: false,
      // 如果使用contextIsolation，就需要在preload中手动提供node这边的方法，
      // 很不方便，最后不采用这个方案，直接使用vueuse的electron插件
      // preload: path.join(__dirname, 'preload.js'),
    },
  });
  win.loadURL('http://localhost:5173');
  if (!app.isPackaged) {
    // Open the DevTools.
    win.webContents.openDevTools({
      mode: 'detach',
    });
  }
}

app.whenReady().then(() => {
  createWindow();

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow();
    }
  });
});

ipcMain.on('windowMinimize', event => {
  const win = BrowserWindow.fromWebContents(event.sender);
  win.minimize();
});
ipcMain.on('windowMaximize', event => {
  const win = BrowserWindow.fromWebContents(event.sender);
  if (win.isMaximized()) {
    win.restore();
  } else {
    win.maximize();
  }
});
ipcMain.on('windowClose', event => {
  const win = BrowserWindow.fromWebContents(event.sender);
  win.close();
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});
